'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminTzfwCtrl
 * @description
 * # AdministratorAdminTzfwCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorAdminTzfwCtrl', function ($scope, Util, DataService, openApi, $location, $routeParams, FormSubmit, localStorageService) {
        var id = ($scope.process_id = $routeParams.id);
        $scope.users = []
        $scope.hurryLevelList = [{
            key: "紧急",
            value: 1
        }, {
            key: "加急",
            value: 2
        }, {
            key: "普通",
            value: 3
        }]
        $scope.secretLevelList = [{
            key: "秘密",
            value: 1
        }, {
            key: "机密",
            value: 2
        }, {
            key: "绝密",
            value: 3
        }, {
            key: "公开",
            value: 4
        }]

        $scope.showType = function () {
            var param = {}
            param.type = "通知发文类型";
            DataService.getData(openApi.getKeyType, param).then(function (data) {
                $scope.typeList = data
            })
        };

        $scope.chooseUsers = function () {
            localStorageService.set("users", $scope.users);
            Util.savaParamForReview($scope.param, id);
            $location.path("administrator/admin_tzfw_users");
        };
        $scope.submit = function (operationId) {

            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.publicdoc;
            options.param = angular.copy($scope.param, {});
            var sendScope = $scope.users.map(function (user) {
                return user.join(":")
            }).join(",")
            options.param.sendScope = sendScope
            FormSubmit.submit(options, $scope);
        };
        $scope.validate = function () {
            if (!$scope.param.docType) {
                Util.Toast("请选择印章类别");
                return;
            }
            if (!$scope.param.docName) {
                Util.Toast("请填写标题");
                return;
            }
            if (!$scope.param.secretLevel) {
                Util.Toast("请选择秘密等级");
                return;
            }
            if (!$scope.param.hurryLevel) {
                Util.Toast("请选择缓急等级");
                return;
            }
            if ($scope.users.length < 1) {
                Util.Toast("请选择查看人员");
                return;
            }

            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }

        var init = function () {
            // 获取回显的详细信息

            Util.getDetailData(id, function (data) {
                $scope.param = data;
                var localUsers = localStorageService.get("users");
                localStorageService.remove("users");
                if (localUsers) {
                    $scope.users = localUsers
                } else {
                    if (data.sendScope) {
                        $scope.users = data.sendScope.split(",").map(function (user) {
                            return user.split(":")
                        })
                    } else {
                        $scope.users = [];
                    }
                }
            });
            $scope.showType();
        };
        // 初始化
        init();
    });
